const Koa = require("koa");
const app = new Koa();
const router = require("koa-router")();
const render = require("koa-art-template");
const { resolve } = require("path");
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017';
render(app, {
    root: resolve(__dirname, "views"),
    extname: ".html"
})
MongoClient.connect(url, { useNewUrlParser: true }, (err, client) => {
    if (err) throw err;
    client.db("gongcheng").collection('student').find().toArray((err, res) => {
        if (err) throw err;
        router.get('/', async ctx => {
            console.log(res);
            await ctx.render('index',{data:res})
        })
        client.close()
    })
})
router.get("/detail", async ctx => {
    await ctx.render("detail")
})
router.get("/news", async ctx => {
    await ctx.render("news")
})
app.use(router.routes());
app.listen(3000);
console.log("服务器运行在http://localhost:3000")